/*
 * System : Ticketing
 * Package: com.ql2.ticketing.interceptor
 * Class: LoginInterceptor
 *
 * © QL2 Software 2011
 */
package main.java.org.dss.lm.admin.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import main.java.org.dss.lm.admin.bean.LoginBean;
import main.java.org.dss.lm.admin.common.ITSConstants;
import main.java.org.dss.lm.admin.dao.login.ILoginService;
import main.java.org.dss.lm.admin.dao.login.LoginService;
import main.java.org.dss.lm.admin.util.LocMgmtUtil;

import org.apache.struts2.ServletActionContext;

import javax.servlet.http.Cookie;
import java.util.Map;

/**
 * This is an interceptor class that extends {@link com.opensymphony.xwork2.interceptor.AbstractInterceptor}
 * and implements {@link com.ql2.ticketing.common.ITSConstants} interface. This class performs
 * session checking for all requests and decides where to move the request next.
 */
public class LoginInterceptor extends AbstractInterceptor implements ITSConstants {
    /**
     * This method checks whether request is coming from an existing session or not. If the request
     * belong to already existing session, 'success' is returned , else invoke the required
     * process.
     *
     * @throws Exception during the session initialization.
     */
    public String intercept(ActionInvocation invocation) throws Exception {

        Map session = invocation.getInvocationContext().getSession();
        
        System.out.println(" 1. I am in login interceptor **************** ");

        // Checking for already logged-in user.
        if (!session.isEmpty()) {
        	System.out.println("2. I am in session method **************** ");
            return SUCCESS;
        }

        // Checking for cookies.
        if (ServletActionContext.getRequest().getCookies() == null) {
        	System.out.println("3. I am in cookies method **************** ");
            return invocation.invoke();
        }

        // Load from cookie and check user.
        LoginBean loginBean = new LoginBean();
        ILoginService loginService = new LoginService();

        for (Cookie cookie : ServletActionContext.getRequest().getCookies()) {
        	System.out.println("5. I am in cookies method **************** ");
            if (COOKIE_USERNAME.equals(cookie.getName())) {
            	System.out.println("6. I am in cookies method **************** ");
            	loginBean.setFrm_username(cookie.getValue());
            }

            if (COOKIE_PASSWORD.equals(cookie.getName())) {
            	System.out.println("7. I am in cookies method **************** ");
            	loginBean.setFrm_password(cookie.getValue());
            }

            if (LocMgmtUtil.isNotNull(loginBean.getFrm_username()) &&
                    LocMgmtUtil.isNotNull(loginBean.getFrm_password())) {
            	System.out.println("8. I am in cookies method **************** ");
                //loginService.setSession(HibernateSessionFactory.getNewSession());
                if (AUTHORIZED.equals(loginService.checkAuthentication(loginBean))) {
                	System.out.println("9. I am in cookies method **************** ");
                    return SUCCESS;
                } else {
                	System.out.println("10. I am in cookies method **************** ");
                    return INPUT;
                }
            }
        }
        // No user in session and cookies. So, invoke the process.
        return invocation.invoke();
    }

} // End Class LoginInterceptor
